package com.dstats.sync;

import android.content.ContentValues;
import android.content.Context;

import com.dstats.data.AppDataContract;
import com.dstats.datamodel.Hero;
import com.dstats.datamodel.Heropedia;
import com.dstats.network.JsonReader;
import com.dstats.network.WebApiClient;

/**
 * Created by MeSeems - Note 1 on 22/01/2015.
 */
public class SyncHero extends SyncAbstractModel {

    public SyncHero(Context context) {
        super(context);
    }

    @Override
    protected void onPull() {
        new WebApiClient(getContext(), "https://api.steampowered.com/IEconDOTA2_570", this)
                .accumulateKey()
                .accumulate("language", "pt")
                .executeGet("v0001", "GetHeroes");
    }

    @Override
    protected void onPush() {

    }

    @Override
    public void onPullFinish(JsonReader response, Object result) {

        super.onPullFinish(response, result);
    }

    @Override
    protected ResultHolder saveResponse(JsonReader response) {
        Hero[] heroes = response.getJsonObject("result").getObject("heroes", Hero[].class);
        for (Hero hero : heroes) {
            getContext().getContentResolver().insert(AppDataContract.AppHeroEntry.CONTENT_URI, hero.getContentValues());
        }
        new SyncHeropedia(getContext(), heroes).pull();
        return new ResultHolder(response, 1);
    }


    @Override
    public void onServerError(JsonReader response) {

    }

    private class SyncHeropedia extends SyncAbstractModel {
        private Hero[] heroes;

        public SyncHeropedia(Context context, Hero[] heroes) {
            super(context);
            this.heroes = heroes;
        }

        @Override
        protected void onPull() {
            new WebApiClient(getContext(), WebApiClient.DOTA_URL, this).execute(WebApiClient.HEROPEDIA, WebApiClient.JSFEED);
        }

        @Override
        protected void onPush() {

        }

        @Override
        protected ResultHolder saveResponse(JsonReader response) {
            for (Hero hero : heroes) {
                Heropedia heropedia = response.getObject(hero.getShortName(), Heropedia.class);
                getContext().getContentResolver().update(
                        AppDataContract.AppHeroEntry.CONTENT_URI, heropedia.getContentValues(),
                        AppDataContract.AppHeroEntry._ID + " =?", new String[]{String.valueOf(hero.getId())});
                for (String str: heropedia.getRoles()){
                    ContentValues values = new ContentValues();
                    values.put(AppDataContract.AppHeroRolesEntry.HERO_ID, hero.getId());
                    values.put(AppDataContract.AppHeroRolesEntry.ROLE, str);
                    getContext().getContentResolver().insert(AppDataContract.AppHeroRolesEntry.CONTENT_URI, values);
                }
            }
            return new ResultHolder(response, 1);
        }

        @Override
        public void onServerError(JsonReader response) {

        }
    }

}
